let connection = false
module.exports = function (mongoose, config) {
  if (!connection) {
    // 连接mongodb
    const uri = `mongodb://${config.user}:${config.password}@${config.host}:${config.port}/${config.database}`
    connection = mongoose.connect(uri, config.options || {})
    mongoose.set('debug', config.debug)
    mongoose.connection.on('connected', () => {
      yy.log.info(`Connect to ${uri} success`)
    })
    mongoose.connection.on('error', (err) => {
      yy.log.error(`Connect to ${uri} failed`)
      yy.log.error(err)
      process.exit(1)
    })
    mongoose.connection.on('disconnected', () => {
      yy.log.info(`Disconnected ${uri}`)
    })
    /*
    process.on('SIGINT', () => {
      yy.log.warn(`Thinkjs catch SIGINT`)
      mongoose.connection.close(() => {
        yy.log.warn('close mongoose')
        process.exit(0)
      })
    })
    */
    return connection
  }
}
